<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
/**
 * Created by PhpStorm.
 * SmsMoban: Administrator
 * Date: 2020/3/14
 * Time: 17:18
 */

namespace app\lnadmin\controller;
use app\core\InitController;
use think\facade\View;
use think\facade\Filesystem;
use app\lnadmin\controller\Upload;
use app\lnadmin\model\admin\System as SystemModel;
use app\lnadmin\model\admin\Image as ImageModel;

class System extends InitController
{

    // 列表
    public function list()
    {
        return View::fetch('system', [
            'sina'        => load_config('extra/config_sina_mail','extra'), 
            'qq'          => load_config('extra/config_qq_mail','extra'), 
            'enterprise'  => SystemModel::where('key','config_enterprise_email')->value('jdata'), 
            'sms'         => load_config('extra/config_sms','extra'), 
            'model'       => SystemModel::where('key','config_system_settings')->find()['jdata'], 
        ]);
    }
 

    // 列表
    public function edit()
    { 
        if ($this->request->isPost()) {
            $param = $this->request->param();
             //保存图片
            if (!empty($param['filesize'])) {
             ImageModel::add([
                    'table_id' => 5,
                    'url' => $param['image'],
                    'types' => Upload::TYPE_SYSTEM,
                    'file_size' => $param['filesize'],
                    'mine' => $param['mine']
             ]);
            }else{
            
                $m = ImageModel::where(['table_id' => 5, 'types' => Upload::TYPE_SYSTEM])->find();
                $param['image'] = $m->url;
                $param['filesize'] = $m->file_size;
                $param['mine'] = $m->mine;
            }
              if ($param['model'] == 'config_enterprise_email' || $param['model'] == 'config_sina_mail' || $param['model'] == 'config_qq_mail' || $param['model'] == 'config_sms') {
                unset($param['image']);
                unset($param['filesize']);
                unset($param['mine']);
               }
            extraConfig($param, '/extra/'.$param['model']);//extraConfig()是自定义函数
            SystemModel::where('key',$param['model'])->update(['jdata'=>$param]);
            
            cache($param['model'],null);
            add_admin_log('edit', '系统设置');// 记录行为日志
            $this->success('', '', __('Set successfully'));
        }
    }


   }